package com.zyh.article.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.zyh.article.domain.Articles;
import com.zyh.article.service.IArticlesService;
import com.zyh.common.annotation.Log;
import com.zyh.common.core.controller.BaseController;
import com.zyh.common.core.domain.AjaxResult;
import com.zyh.common.core.page.TableDataInfo;
import com.zyh.common.enums.BusinessType;
import com.zyh.common.utils.SecurityUtils;
import com.zyh.common.utils.poi.ExcelUtil;
import com.zyh.websocket.service.MessageSenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 文章管理Controller
 *
 * @author zhangyangheng
 * @date 2025-03-15
 */
@RestController
@RequestMapping("/articles/articles")
public class ArticlesController extends BaseController {
    @Autowired
    private IArticlesService articlesService;

    @Autowired
    private MessageSenderService messageSenderService;

    /**
     * 获取文章点赞数
     *
     * @param articleId
     * @return
     */
    @PutMapping("/like/{articleId}")
    public AjaxResult like(@PathVariable("articleId") Long articleId) {
        logger.info("like入参->{}", articleId);
        return toAjax(articlesService.like(articleId));
    }

    /**
     * 首页展示三个文章
     *
     * @return
     */
    @GetMapping("/getIndexArticle")
    public TableDataInfo getIndexArticle() {
        List<Articles> list = articlesService.list(new LambdaQueryWrapper<Articles>()
                .eq(Articles::getStatus, "2")
                .eq(Articles::getIsPublish, "1")
                .eq(Articles::getDelFlag, "0")
                // 根据观看人数升序排序
                .orderByAsc(Articles::getViewCount)
                // 取前三篇文章
                .last("limit 3"));
        return getDataTable(list);
    }

    /**
     * 根据id获取文章详情
     *
     * @param articleId
     * @return
     */
    @GetMapping("/getArticleDetail/{articleId}")
    public AjaxResult getArticleDetail(@PathVariable("articleId") Long articleId) {
        logger.info("getArticleDetail->{}", articleId);
        Articles articles = articlesService.getOne(new LambdaUpdateWrapper<Articles>()
                .eq(Articles::getArticleId, articleId)
                .eq(Articles::getStatus, "2")
                .eq(Articles::getIsPublish, "1")
                .eq(Articles::getDelFlag, "0"));
        return Objects.isNull(articles) ? AjaxResult.error("文章不存在") : AjaxResult.success(articles);
    }

    /**
     * 获取我的文章
     *
     * @return
     */
    @GetMapping("/getMyArticles")
    public TableDataInfo getMyArticles() {
        startPage();
        List<Articles> list = articlesService.list(new LambdaQueryWrapper<Articles>()
                .eq(Articles::getCreateBy, SecurityUtils.getUsername())
                .eq(Articles::getDelFlag, "0"));
        return getDataTable(list);
    }

    /**
     * 获取所有已发布文章
     *
     * @return
     */
    @GetMapping("/getReviewedArticles")
    public TableDataInfo getReviewedArticles() {
        startPage();
        List<Articles> list = articlesService.list(new LambdaQueryWrapper<Articles>()
                .eq(Articles::getStatus, "2")
                .eq(Articles::getIsPublish, "1")
                .eq(Articles::getDelFlag, "0"));
        return getDataTable(list);
    }

    /**
     * 更改审核状态
     */
    @PutMapping("/changeArticleStatus")
    public AjaxResult changeArticleStatus(@RequestBody Articles articles) {
        logger.info("changeArticleStatus入参->{}", articles);
        return articlesService.changeArticleStatus(articles);
    }

    /**
     * 获取所有待审批的文章
     *
     * @return
     */
    @GetMapping("/getReviewArticles")
    public TableDataInfo getReviewArticles() {
        startPage();
        List<Articles> list = articlesService.list(new LambdaQueryWrapper<Articles>()
                .eq(Articles::getStatus, "1")
                .eq(Articles::getIsPublish, "1")
                .eq(Articles::getDelFlag, "0"));
        return getDataTable(list);
    }

    /**
     * 更改文章发布状态
     *
     * @param articles
     * @return
     */
    @PutMapping("/changePublishStatus")
    public AjaxResult changePublishStatus(@RequestBody Articles articles) {
        logger.info("changePublishStatus入参->{}", articles);
        return toAjax(articlesService.changePublishStatus(articles));
    }

    /**
     * 管理员查询文章管理列表
     */
    //@PreAuthorize("@ss.hasPermi('articles:articles:list')")
    @GetMapping("/listAll")
    public TableDataInfo listAll(Articles articles) {
        startPage();
        List<Articles> list = articlesService.selectArticlesListAll(articles);
        return getDataTable(list);
    }

    /**
     * 自己查询文章管理列表
     */
    //@PreAuthorize("@ss.hasPermi('articles:articles:list')")
    @GetMapping("/list")
    public TableDataInfo list(Articles articles) {
        startPage();
        List<Articles> list = articlesService.selectArticlesList(articles);
        return getDataTable(list);
    }

    /**
     * 导出文章管理列表
     */
    //@PreAuthorize("@ss.hasPermi('articles:articles:export')")
    @Log(title = "文章管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Articles articles) {
        List<Articles> list = articlesService.selectArticlesList(articles);
        ExcelUtil<Articles> util = new ExcelUtil<Articles>(Articles.class);
        util.exportExcel(response, list, "文章管理数据");
    }

    /**
     * 获取文章管理详细信息
     */
    //@PreAuthorize("@ss.hasPermi('articles:articles:query')")
    @GetMapping(value = "/{articleId}")
    public AjaxResult getInfo(@PathVariable("articleId") Long articleId) {
        return success(articlesService.selectArticlesByArticleId(articleId));
    }

    /**
     * 新增文章管理
     */
    //@PreAuthorize("@ss.hasPermi('articles:articles:add')")
    @Log(title = "文章管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Articles articles) {
        articles.setCreateBy(SecurityUtils.getUsername());
        articles.setCreateTime(new Date());
        logger.info("ArticlesController-->add入参->{}", articles);
        return toAjax(articlesService.insertArticles(articles));
    }

    /**
     * 修改文章管理
     */
    //@PreAuthorize("@ss.hasPermi('articles:articles:edit')")
    @Log(title = "文章管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Articles articles) {
        return toAjax(articlesService.updateArticles(articles));
    }

    /**
     * 删除文章管理
     */
    //@PreAuthorize("@ss.hasPermi('articles:articles:remove')")
    @Log(title = "文章管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{articleIds}")
    public AjaxResult remove(@PathVariable Long[] articleIds) {
        return toAjax(articlesService.deleteArticlesByArticleIds(articleIds));
    }
}
